﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SisLegal.Model;

namespace SisLegal.ExpedientFiled
{
    public partial class ExpedientFiledDisplayForm : System.Web.UI.Page
    {
        private int ID
        {
            get
            {
                if (Request.QueryString["id"] == null)
                    return 0;
                else
                    return int.Parse(Request.QueryString["id"]);
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                hlUpload.NavigateUrl = "~/ExpedientFiled/ExpedientFiledUploadForm.aspx?id=" + ID.ToString() + "&type=" + (int)eFileLoad.Expediente_Principal;
                hlUploadSecond.NavigateUrl = "~/ExpedientFiled/ExpedientFiledUploadForm.aspx?id=" + ID.ToString() + "&type=" + (int)eFileLoad.Expediente_Secundary;
                hlEdit.NavigateUrl = "~/ExpedientFiled/ExpedientFiledEditForm.aspx?id=" + ID.ToString();
                

                FileLoadManager file = new FileLoadManager();
                FilePrincipal.DataSource = file.GetAllFile(ID, (int)eFileLoad.Expediente_Principal);
                FilePrincipal.DataBind();

                FileSecundary.DataSource = file.GetAllFile(ID, (int)eFileLoad.Expediente_Secundary);
                FileSecundary.DataBind();

                DDL_Type_Process.DataSource = Enumeration.GetAll<eTypeProcess>();
                DDL_Type_Process.DataTextField = "Value";
                DDL_Type_Process.DataValueField = "Key";
                DDL_Type_Process.DataBind();

                DDL_ResultSentences.DataSource = Enumeration.GetAll<eResultSentences>();
                DDL_ResultSentences.DataTextField = "Value";
                DDL_ResultSentences.DataValueField = "Key";
                DDL_ResultSentences.DataBind();

                DDL_Participation_Seal.DataSource = Enumeration.GetAll<eParticipation_Seal_Fiscal_Folder>();
                DDL_Participation_Seal.DataTextField = "Value";
                DDL_Participation_Seal.DataValueField = "Key";
                DDL_Participation_Seal.DataBind();


                //---------------------------------------------------------
                Model.ExpedientFiledManager manager = new Model.ExpedientFiledManager();
                Model.ExpedientFiled obj = manager.GetById(ID);

                this.txtNroExpedient.Text = obj.NroExpedient;
                DDL_Type_Process.ClearSelection();
                DDL_Type_Process.Items.FindByValue(obj.TypeProcess.ToString()).Selected = true;

                Model.ViaProceduralManager ViaProcedural = new Model.ViaProceduralManager();
                ddlViaProcedural.DataSource = ViaProcedural.GetByProcessID(Convert.ToInt32(DDL_Type_Process.SelectedItem.Value));
                ddlViaProcedural.DataTextField = "Name";
                ddlViaProcedural.DataValueField = "ID";
                ddlViaProcedural.DataBind();

                ddlViaProcedural.ClearSelection();
                ddlViaProcedural.Items.FindByValue(obj.ViaProcedural.ToString()).Selected = true;

                this.txtDateBegin.Text = String.Format("{0:dd/MM/yyyy}", obj.DateBegin);
                this.txtDateEnd.Text = String.Format("{0:dd/MM/yyyy}", obj.DateEnd);

                DDL_ResultSentences.ClearSelection();
                DDL_ResultSentences.Items.FindByValue(obj.ResultSentences.ToString()).Selected = true;

                this.txtJuzgado.Text = obj.Juzgado;
                this.txtPretencion.Text = obj.Pretencion;
                this.txtDemandante.Text = obj.Demandante;
                this.txtDemandado.Text = obj.Demandado;

                DDL_Participation_Seal.ClearSelection();
                DDL_Participation_Seal.Items.FindByValue(obj.ParticipationSeal.ToString()).Selected = true;

                this.txtLawyerOther.Text = obj.LawyerOther;
                this.txtSumilla.Text = obj.Sumilla;
                this.txtObservaciones.Text = obj.Observaciones;


            }
        }

        protected void Delete_Click(object sender, EventArgs e)
        {
            Model.ExpedientFiledManager manager = new Model.ExpedientFiledManager();  
            manager.Delete(ID);
            Response.Redirect("~/ExpedientFiled/ExpedientFiledListView.aspx");

        }

        protected void Index_Changed(Object sender, EventArgs e)
        {
            Model.ViaProceduralManager manager = new Model.ViaProceduralManager();
            ddlViaProcedural.DataSource = manager.GetByProcessID(Convert.ToInt32(DDL_Type_Process.SelectedItem.Value));
            ddlViaProcedural.DataTextField = "Name";
            ddlViaProcedural.DataValueField = "ID";
            ddlViaProcedural.DataBind();
        }

        protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            Int32 idFile = Convert.ToInt32(e.CommandArgument);
            if (e.CommandName == "download")
            {
                Download(idFile);
            }
            if (e.CommandName == "delete")
            {
                Model.FileLoadManager manager = new Model.FileLoadManager();
                manager.Delete(idFile);

                FileLoadManager file = new FileLoadManager();
                FilePrincipal.DataSource = file.GetAllFile(ID, (int)eFileLoad.Expediente_Principal);
                FilePrincipal.DataBind();

                FileSecundary.DataSource = file.GetAllFile(ID, (int)eFileLoad.Expediente_Secundary);
                FileSecundary.DataBind();
            }
        }

        public void Download(int ID)
        {
            Model.FileLoadManager manager = new Model.FileLoadManager();
            FileLoad file = manager.GetById(ID);

            string content_type = "";

            if (String.ReferenceEquals(file.Ext, ".htm") || String.ReferenceEquals(file.Ext, ".html"))
            {
                content_type = "text/HTML";
            }
            if (String.ReferenceEquals(file.Ext, ".txt"))
            {
                content_type = "text/plain";
            }
            if (String.ReferenceEquals(file.Ext, ".doc") || String.ReferenceEquals(file.Ext, ".rtf") || String.ReferenceEquals(file.Ext, ".docx"))
            {
                content_type = "Application/msword";
            }
            if (String.ReferenceEquals(file.Ext, ".xls") || String.ReferenceEquals(file.Ext, ".xlsx"))
            {
                content_type = "Application/x-msexcel";
            }
            if (String.ReferenceEquals(file.Ext, ".jpg") || String.ReferenceEquals(file.Ext, ".jpeg"))
            {
                content_type = "image/jpeg";
            }
            if (String.ReferenceEquals(file.Ext, ".gif"))
            {
                content_type = "image/GIF";
            }
            if (String.ReferenceEquals(file.Ext, ".pdf"))
            {
                content_type = "Application/pdf";
            }

            Response.ContentType = content_type;
            Response.AppendHeader("Content-Disposition", "attachment; filename=" + file.Name);
            Response.TransmitFile(Server.MapPath("~/Uploads/" + file.Guid));
            Response.End();
        }
    }
}